package cn.picclife.mwx.manager.sys.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//import cn.picclife.mwx.common.web.controller.ApiBaseController;
import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.sys.model.bo.CommonBo;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.RolesBo;
import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import cn.picclife.mwx.manager.sys.model.po.Enum;
import cn.picclife.mwx.manager.sys.service.RolesMgMtService;
import cn.picclife.mwx.manager.util.RestUtils;

/**
 * 
 * @Title RolesMgMtController
 * @Author cc
 * @Description:角色管理功能
 * @Param
 * @date 2016年12月9日 下午12:33:17
 */
@Controller
@RequestMapping(value = "/api")
public class RolesMgMtApiController /*extends ApiBaseController*/{
	@Autowired
	private RolesMgMtService rolesMgMtService;

	/**
	 * 角色信息查询
	 * 
	 * @param request
	 * @param pageCount
	 * @return
	 */
	@RequestMapping(value = "/roles/query_roles_page")
	@ResponseBody
	public PageCount queryRolePage(HttpServletRequest request, PageCount pageCount) {
		PageCount pageQuery = rolesMgMtService.queryRolesPage(pageCount);
		return pageQuery;

	}

	/**
	 * 角色信息添加
	 * 
	 * @param request
	 * @param response
	 * @param rolesBo
	 * @return
	 */
	@RequestMapping(value = "/roles/add_roles")
	@ResponseBody
	public BaseResult1 addRoles(HttpServletRequest request, HttpServletResponse response, RolesBo rolesBo) {
		BaseResult1 result = new BaseResult1();
		UserSessionBo userSessionBo = (UserSessionBo)request.getSession().getAttribute("user");
		String user = userSessionBo.getOptNo();
		rolesBo.setCrtUser(user);
		
		if (StringUtils.isBlank(rolesBo.getRoleNo())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("角色编码不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(rolesBo.getRoleName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("角色名称不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(rolesBo.getRoleType())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("角色类型不能为空");
			RestUtils.error(response, result);
			return null;
		}
		RolesBo roles = rolesMgMtService.queryCountRoles(rolesBo.getRoleNo());
		if (null == roles) {
			int count = rolesMgMtService.insertRoles(rolesBo);
			if (count > 0) {
				result.setRet(BaseResult1.SUCCESS);
				result.setMsg("添加成功");
				return result;
			} else {
				result.setRet(BaseResult1.BIZ_ERROR);
				result.setMsg("添加失败");
				RestUtils.error(response, result);
				return null;
			}
		} else {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("角色编码已存在");
			RestUtils.error(response, result);
			return null;
		}
	}

	/**
	 * 查询要修改的角色信息
	 * 
	 * @param request
	 * @param roleNo
	 * @return
	 */
	@RequestMapping(value = "/roles/get_roles_info")
	@ResponseBody
	public RolesBo getRolesInfo(HttpServletRequest request, String roleNo) {
		RolesBo queryRoles = rolesMgMtService.queryModifyRoles(roleNo);
		return queryRoles;
	}

	/**
	 * 保存修改后的角色信息
	 * 
	 * @param request
	 * @param response
	 * @param rolesBo
	 * @return
	 */
	@RequestMapping(value = "/roles/modify_roles")
	@ResponseBody
	public BaseResult1 ModifyRoles(HttpServletRequest request, HttpServletResponse response, RolesBo rolesBo) {
		BaseResult1 result = new BaseResult1();
		UserSessionBo userSessionBo = (UserSessionBo)request.getSession().getAttribute("user");
		String optNo = userSessionBo.getOptNo();
		rolesBo.setMdfUser(optNo);

		int count = rolesMgMtService.updateRoles(rolesBo);
		if (count > 0) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("修改成功");
			return result;
		} else {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("修改失败");
			RestUtils.error(response, result);
			return null;
		}
	}

	/**
	 * 查询角色菜单信息
	 * 
	 * @param request
	 * @param rolesBo
	 * @return
	 */
	@RequestMapping(value = "/roles/query_roles_menu")
	@ResponseBody
	public List<Map<String, Object>> queryRoleMenu(HttpServletRequest request, RolesBo rolesBo) {
		List<Map<String, Object>> rolesMenuBo = rolesMgMtService.queryRolesMenu(rolesBo);
		return rolesMenuBo;

	}

	/**
	 * 修改角色菜单信息
	 * 
	 * @param request
	 * @param rolesBo
	 * @return
	 */
	@RequestMapping(value = "/roles/mdf_roles_menu")
	@ResponseBody
	public BaseResult1 mdfRolesMenu(HttpServletRequest request, HttpServletResponse response, CommonBo commonBo) {
		BaseResult1 result = new BaseResult1();
		UserSessionBo userSessionBo = (UserSessionBo)request.getSession().getAttribute("user");
		String user = userSessionBo.getOptNo();

		int count = rolesMgMtService.saveRolesMenu(commonBo, user);
		if (count > 0) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("保存成功");
			return result;
		} else {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("保存失败");
			RestUtils.error(response, result);
			return null;
		}

	}

	/**
	 * 查询用户角色信息
	 * 
	 * @param request
	 * @param pageCount
	 * @return
	 */
	@RequestMapping(value = "/roles/query_roles_user")
	@ResponseBody
	public PageCount queryRolesUser(HttpServletRequest request, PageCount pageCount) {
		UserSessionBo branchUser =(UserSessionBo) request.getSession().getAttribute("user");
		pageCount.getObject().put("branchId", branchUser.getDeptNo());
		PageCount pageQuery = rolesMgMtService.queryRolesUser(pageCount);
		return pageQuery;

	}

	/**
	 * 保存用户角色信息
	 * 
	 * @param request
	 * @param response
	 * @param rolesList
	 * @return
	 */
	@RequestMapping(value = "/roles/save_roles_user")
	@ResponseBody
	public BaseResult1 saveRolesUser(HttpServletRequest request, HttpServletResponse response, CommonBo commonBo) {
		BaseResult1 result = new BaseResult1();
		UserSessionBo userSessionBo = (UserSessionBo)request.getSession().getAttribute("user");
		String user = userSessionBo.getOptNo();

		int count = rolesMgMtService.saveRolesUser(commonBo, user);
		if (count > 0) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("保存成功");
			return result;
		} else {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("保存失败");
			RestUtils.error(response, result);
			return null;
		}

	}
	@RequestMapping(value = "/roles/get_role_type")
	@ResponseBody
	public List<Enum> getEnumInfo(HttpServletRequest request) {
		List<Enum> roleTtypeList= rolesMgMtService.queryRoleType();
		return roleTtypeList;
	}
	@RequestMapping(value = "/roles/get_agentCate_type")
	@ResponseBody
	public List<Enum> getAgentCateEnumInfo(HttpServletRequest request) {
		List<Enum> agentCateTtypeList= rolesMgMtService.queryAgentCateType();
		return agentCateTtypeList;
	}
	
	
	@RequestMapping("/roles/delete_role")
	@ResponseBody
	public BaseResult1 deleteUser(HttpServletRequest req, HttpServletResponse res, CommonBo commonBo) {
		BaseResult1 result = new BaseResult1();
		if(commonBo.getDatas().size()<1){
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("请至少选择一个角色");
			RestUtils.error(res, result);
			return result;
		}
		try {
			int row = rolesMgMtService.deleteUser(req, res, commonBo);
			if (row > 0) {
				result.setRet(BaseResult1.SUCCESS);
				result.setMsg("删除成功");
				return result;
			} else {
				result.setRet(BaseResult1.BIZ_ERROR);
				result.setMsg("删除失败");
				RestUtils.error(res, result);
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setRet(BaseResult1.SYS_ERROR);
			result.setMsg("删除失败");
			RestUtils.error(res, result);
			return null;
		}
	}
	
}
